﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Draco.Commons.Container
{
    /// <summary>
    /// 包装的System.Collections.Generic.Dictionary,避免使用key索引获取Value的时候抛出异常
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    public class DictionaryEx<TKey, TValue> : Dictionary<TKey, TValue>
    {
        /// <summary>
        /// 构造
        /// </summary>
        public DictionaryEx(){}
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dictionary"></param>
        public DictionaryEx(IDictionary<TKey, TValue> dictionary):base(dictionary){}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="comparer"></param>
        public DictionaryEx(IEqualityComparer<TKey> comparer) : base(comparer) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="capacity"></param>
        public DictionaryEx(int capacity):base(capacity){}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dictionary"></param>
        /// <param name="comparer"></param>
        public DictionaryEx(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
            :base(dictionary,comparer){}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="capacity"></param>
        /// <param name="comparer"></param>
        public DictionaryEx(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public new TValue this[TKey key] 
        {
            get 
            {
                if (this.ContainsKey(key))
                    return base[key];
                return default(TValue);
            }
            set { base[key] = value; }
        }
    }
}
